package org.wangle.order.publisher;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Component;
import org.wangle.api.order.pojo.Orders;
import org.wangle.api.properties.MQ.SecKillProperties;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.Objects;


@Slf4j
@Component
public class SecKillPublisher {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(Orders msg){
        if(!Objects.isNull(msg)){
            try {
                log.info("用户【"+msg.getUserId()+"】发送欲购买订单消息");
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.setExchange(SecKillProperties.secKillExchange);
                rabbitTemplate.convertAndSend(SecKillProperties.secKillBindingKey, JSON.toJSONString(msg));
            }catch (Exception e){
                log.info("基本消息模型-生产者-发送消息异常：{}",msg.toString(),e.fillInStackTrace());

            }
        }

    }
}
